<?php

namespace App\Http\Services\FormBuilder\Components;

use App\Http\Services\FormBuilder\Elements\Field;
use App\Http\Services\FormBuilder\Segments\BracketDonate;
use App\Http\Services\FormBuilder\Segments\QuotaDonate;
use App\Http\Services\FormBuilder\Traits\MoneySelectorTrait;

class MoneySelector extends AbstractComponent
{
    use MoneySelectorTrait;

    private $quotaDonateName;
    private $label;

    public function __construct(array $data)
    {
        $this->data = collect($data);
    }

    public function setQuotaDonateName($quotaDonateName)
    {
        $this->quotaDonateName = $quotaDonateName;

        return $this;
    }

    public function withLabel($label)
    {
        $this->label = $label;

        return $this;
    }

    public function renderQuotaDonate()
    {
        $quotaMoney  = $this->getDefaultMoney() ?: $this->data->get('quota_money');

        $quotaDonate = (new QuotaDonate($this->name, $this->data))
            ->interval($this->interval)
            ->setMoney($quotaMoney)
            ->setQuotaDonateName($this->quotaDonateName)
            ->unit($this->getUnit());

        $field       = new Field($quotaDonate);
        $field->id('quota_donate');

        return (string)$field;
    }

    public function renderBracketDonate()
    {
        $bracketDonate = (new BracketDonate($this->name, $this->data))->unit($this->getUnit());

        if (!empty($this->defaultMoney)) {
            $bracketDonate->setDefaultMoney($this->defaultMoney);
        }

        if ($this->showOptionsDesc) {
            $bracketDonate->showOptionsDesc();
        }

        if ($this->label) {
            $bracketDonate->withLabel($this->label);
        }

        $field = new Field($bracketDonate);
        $field->id('money_selector');

        return $field->render();
    }

    public function getUnit()
    {
        $this->intervalDonateInit();
        $result = $this->unit;
        if ($this->isIntervalDonate()) {
            $result .= '/' . $this->intervalDisplayName;
        }

        return $result;
    }

    public function render()
    {
        if ($this->isQuotaDonate()) {
            return $this->renderQuotaDonate();
        } else {
            return $this->renderBracketDonate();
        }
    }

    public function getValidation($name)
    {
        $rule = [
            'identifier' => $name,
            'rules'      => [
                [
                    'type'   => 'empty',
                    'prompt' => '金额超出',
                ],
                [
                    'type'   => 'money',
                    'prompt' => '金额不对',
                ],
            ],
        ];
        if ($this->allowCustomMoney()) {
            $minMoney        = $this->getMinMoney();

            if ($minMoney === '') {
                $rule['rules'] = [];
            } else {
                $rule['rules'][] = [
                    'type'   => 'min[' . $minMoney . ']',
                    'prompt' => 'xxxxx',
                ];
            }

            $maxPeriodicMoney = $this->getMaxPeriodicMoney();
            if ($maxPeriodicMoney) {
                $rule['rules'][] = [
                    'type'   => 'max[' . $maxPeriodicMoney . ']',
                    'prompt' => 'ccccc',

                ];
            }
        }

        return $rule;
    }
}
